package com.youmingxing.activity.selfInfo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.youmingxing.R;
import com.youmingxing.activity.BaseActivity;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.network.SelfInfoApi;

import org.json.JSONObject;

/**
 * @author evilyin(ChenZhixi)
 * @since 2015-10-13
 */

public class ModifyBodyActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_modify_body);
        setTopbarName(R.string.edit);
        String bodyStr = getIntent().getExtras().getString("text");


        final EditText height = (EditText) findViewById(R.id.height);
        final EditText weight = (EditText) findViewById(R.id.weight);
        final EditText breast = (EditText) findViewById(R.id.breast);
        final EditText waist = (EditText) findViewById(R.id.waist);
        final EditText hip = (EditText) findViewById(R.id.hip);

        if (bodyStr != null && !bodyStr.isEmpty()) {
            bodyStr = bodyStr.replace("cm", ",");
            bodyStr = bodyStr.replace("kg", ",");
            bodyStr = bodyStr.replace(" ", "");
            String[] temp = bodyStr.split(",");
            if (temp.length > 0) {
                height.setText(temp[0].equals("0") ? "" : temp[0]);
            }
            if (temp.length > 1) {

                weight.setText(temp[1].equals("0") ? "" : temp[1]);
            }
            if (temp.length > 2) {

                breast.setText(temp[2]);
            }
            if (temp.length > 3) {
                waist.setText(temp[3]);
            }
            if (temp.length > 4) {
                hip.setText(temp[4]);
            }

        }
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String heightString = height.getText().toString().isEmpty() ? "0" : height.getText()
                        .toString();
                String weightString = weight.getText().toString().isEmpty() ? "0" : weight.getText()
                        .toString();
                String bodyString;
                if (breast.getText().toString().isEmpty() && waist.getText().toString().isEmpty()
                        && hip.getText().toString().isEmpty()) {
                    bodyString = "";
                } else if (!breast.getText().toString().isEmpty() && !waist.getText().toString()
                        .isEmpty()
                        && !hip.getText().toString().isEmpty()) {
                    bodyString = breast.getText().toString() + "," + waist.getText().toString() + "," +
                            hip.getText().toString();
                } else {
                    showToast("请输入完整的三围信息哦");
                    return;
                }
                String[] options = {"height", "weight", "bwh"};
                String[] values = {heightString, weightString, bodyString};
                SelfInfoApi.modifyMultiProfile(options, values, new BaseJsonHandler
                        (ModifyBodyActivity.this) {
                    @Override
                    public void onSuccessWithData(JSONObject response) {
                        if (response.optJSONObject("data").optInt("result") == 1) {
                            showToast("修改成功");
                        }
                    }
                });

                Intent intent = new Intent();
                String body = " " + heightString + "cm " + weightString + "kg " + bodyString;
                intent.putExtra("body", body);
                setResult(RESULT_OK, intent);
                finish();

            }
        };
        setLeftAndrightTv(true, true, false, R.string.finish, null, listener, null);
    }

}
